package design.pattern.singleton.lazy;

import java.lang.reflect.Constructor;

/**
 * @description: 反射破坏单例
 * @author: liuzhangcheng
 **/
public class LazyInnerClassSingletonTest {

    public static void main(String[] args) {
        try {
            Class<?> clazz = LazyInnerClassSingleton.class;

            // 通过反射拿到私有的构造方法
            Constructor c = clazz.getDeclaredConstructor(null);
            // 强制访问
            c.setAccessible(true);
            // 暴力初始化
            Object obj1 = c.newInstance();
            // 调用了两次构造方法，相当于 new 了两次,犯了原则性问题，
            Object obj2 = c.newInstance();

            System.out.println(obj1 == obj2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
